package com.gxa.modules.service.impl;

import ch.qos.logback.core.joran.spi.ConsoleTarget;
import com.gxa.common.constant.Constants;
import com.gxa.common.exception.LcException;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.DeptMapper;
import com.gxa.modules.pojo.Dept;
import com.gxa.modules.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: microloans_system
 * @description: 部门相关
 * @author: LC
 * @create: 2022-01-04 09:20
 */

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired(required = false)
    DeptMapper deptMapper;


    @Override
    public ResultVo getDeptPage(PageRecv pageRecv) {
        ResultVo r=new ResultVo();
        List<Dept> deptList=deptMapper.queryDeptPage(pageRecv);
        r.setCode(200);
        r.setData(deptList);
        return r;
    }


    @Override
    public ResultVo addDept(Dept dept) {
        ResultVo r=new ResultVo();
        dept.setDept_date_create(new Date());
        dept.setDept_enable(Constants.DEPT_ENABLE_STATUS);
        int insertResult=deptMapper.insertDept(dept);
        if(insertResult<=0){
            throw new LcException(500,"添加部门失败");
        }
        r.setCode(200);
        r.setMsg("添加部门成功");
        return r;
    }


    @Override
    public ResultVo modifyDept(Dept dept) {
        ResultVo r=new ResultVo();
        int udpateResult=deptMapper.updateDept(dept);
        if(udpateResult<=0){
            throw new LcException(500,"修改部门失败");
        }
        r.setCode(200);
        r.setMsg("修改部门成功");
        return r;
    }

    @Override
    public ResultVo delDept(Dept dept) {
        ResultVo r=new ResultVo();
        dept.setDept_enable(Constants.DEPT_DEL_STATUS);
        int udpateResult=deptMapper.updateDept(dept);
        if(udpateResult<=0){
            throw new LcException(500,"删除部门失败");
        }
        r.setCode(200);
        r.setMsg("删除部门成功");
        return r;
    }

    @Override
    public ResultVo getMemberCount(Dept dept) {
        ResultVo r=new ResultVo();
        long count=deptMapper.queryUserCountByDeptId(dept.getDept_id());
        if(count<0){
            throw new LcException(500,"查询数量失败");
        }
        r.setCode(200);
        r.setMsg("查询数量成功");
        r.setData(count);
        return r;
    }
}